package sharewithus.content.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.rubyeye.xmemcached.GetsResponse;

import org.springframework.beans.factory.annotation.Autowired;

import sharewithus.business.dto.RelationShipMsg;
import sharewithus.content.dao.IFeedCache;
import sharewithus.content.dao.IFeedDao;
import sharewithus.content.dao.IFeedRelationshipCache;
import sharewithus.content.dao.IFeedRelationshipDao;
import sharewithus.content.dto.Feed;
import sharewithus.content.dto.FeedEx;
import sharewithus.content.dto.FeedRelationship;
import sharewithus.content.service.IContentService;

public class ContentServiceImpl implements IContentService{


	@Autowired
	private IFeedDao feedDao;
	
	@Autowired
	private IFeedCache feedCache;
	
	@Autowired
	private IFeedRelationshipDao frDao;
	
	@Autowired
	private IFeedRelationshipCache frCache;
	
	public void saveFeed(Feed feed){
		//save feed to db
		feedDao.saveFeed(feed);
		
		//save feed to memcached
		feedCache.saveFeed(feed);
		
	}

	public void pushFeeds2Subscribers(Feed feed,
			List<RelationShipMsg> followRelationships) {
		
		Iterator<RelationShipMsg>  iter1 = followRelationships.iterator();
		RelationShipMsg rsm = null;
		//save to redis cache
		while(iter1.hasNext()){
			rsm = iter1.next();
			String subScriberId = rsm.getFollower();
			frCache.saveSubscriberFeeds(subScriberId, feed);
		}
		
		//save to db
		Iterator<RelationShipMsg>  iter2 = followRelationships.iterator();
		while(iter1.hasNext()){
			rsm = iter1.next();
			String subScriberId = rsm.getFollower();
			frDao.saveSubscriberFeeds(subScriberId, feed);
		}
	}
	
	public void pushOwnerFeed(Feed feed){
		//save feed to db
		
		//save feed to memcached
		feedCache.pushOwnerFeedRelationship(feed);
	}

	public void pullFeeds(List<Feed> feeds, RelationShipMsg newFollowMsg) {
		Iterator<Feed>  iter1 = feeds.iterator();
		Feed feed = null;
		while(iter1.hasNext()){
			feed = iter1.next();
			frDao.saveSubscriberFeeds(newFollowMsg.getFollower(), feed);
		}
		
		Iterator<Feed>  iter2 = feeds.iterator();
		while(iter1.hasNext()){
			feed = iter1.next();
			frCache.saveSubscriberFeeds(newFollowMsg.getFollower(), feed);
		}
		
	}
	
	public List<Feed> getSubscribedFeeds(String subscriberId,
			int pagination, int pageSize) {
		List<FeedRelationship> frs = null;
		
		frs  = frCache.getSubscribedFeeds(subscriberId);
		
		return generateFeeds(frs);
	}

	
	
	public List<Feed> getOwnerFeeds(String ownerId){
		
		return getOwnerFeeds(ownerId, -1, -1);
	}
	
	public List<Feed> getOwnerFeeds(String ownerId, int pagination, int pageSize){
		List<FeedRelationship> frs = null;
		
		frs  = frCache.getOwnerFeedPage(ownerId);
		return generateFeeds(frs);
	}

	private List<Feed> generateFeeds(List<FeedRelationship> frs) {
		List<Feed> feeds = new ArrayList<Feed>();
		List<String> feedIds = new ArrayList<String>();
		Iterator<FeedRelationship> iter = frs.iterator();
		FeedRelationship fr = null;
		while(iter.hasNext()){
			fr = iter.next();
			feedIds.add(fr.getFeedId());
		}
		Map<String, GetsResponse<Feed>> cacheFeedsRes = feedCache.getFeedbyIds(feedIds);
		Feed cachedFeed = null;
		for (String feedId: feedIds){
			cachedFeed = cacheFeedsRes.get(feedId).getValue();
			if(cachedFeed==null){
				cachedFeed = feedDao.getFeedById(feedId);
			}
			feeds.add(cachedFeed);
		}
		return feeds;
	}
}
